<?php

require_once "lib/Global.php";
require_once "const.inc";

class Helper_Groups extends Zend_Controller_Action_Helper_Abstract
{   
    public function getCatName($cid)
    {
       $c = AppGlobal::loadModel('Category','group');
       return $c->getCatName($cid);
    }
    
    public function getGroupName($gid){
       $c = AppGlobal::loadModel('Groups','group');
       return $c->getGroupName($gid);      
    }
    
    public function getGroupCat($gid)
    {
       $c = AppGlobal::loadModel('Groups','group');
       return $c->getGroupCat($gid);
    }
    
    public function getMemberStatus($uid,$gid){
       $c = AppGlobal::loadModel('GroupMembers','group');
       return $c->getMemberStatus($uid,$gid);            
    }
    
    public function getGroupInfo($gid){
      $ctrl = $this->getActionController();
      $group_detail = $ctrl->getCache("group_detail_$gid","helper_");
      if (!isset($group_detail)||!is_array($group_detail))
        $group_detail = array();
      
      $data_changed = false;
      $g_model = AppGlobal::loadModel('Groups','group');
      
      if (!isset($group_detail['group_info'])){
        $group_info = $g_model->getGroup($gid);
        if ($group_info!=null){
          $data_changed = true;
          $group_detail['group_info'] = $group_info;
        }
      } else {
        $group_info = $group_detail['group_info'];
      }
      
      if ($data_changed){
        $ctrl->setCache("group_detail_$gid",$group_detail,"helper_");
      }
      
      return $group_info;
    }
    
    public function getGroupAdmin($gid){
      $ctrl = $this->getActionController();
      
      $admins = $ctrl->getCache("group_admins_$gid","helper_");
      if (!isset($admins)||!is_array($admins)){
        $g_model = AppGlobal::loadModel('GroupMembers','group');
        $admins = $g_model->getGroupAdmins($gid);
        if ($admins!=null){
          $ctrl->setCache("group_admins_$gid",$admins,"helper_");
        }
      }
      
      return $admins;
    }
    
    public function getGroupCategories(){
      $ctrl = $this->getActionController();
      $data = $ctrl->getCache("fgroup_categories","helper_");
      
      if (!isset($data) || !is_array($data)){
        $c_model = AppGlobal::loadModel("Category",'group');
        $data = $c_model->getCategories();
        $ctrl->setCache("fgroup_categories", $data,"helper_");
      }
      
      return $data;
    }
    
    public function getUserGroups($uid, $page=0, $limit=20){
      $ctrl = $this->getActionController(); 
      
      $user_groups = $ctrl->getCache("user_groups_$uid","helper_");
      if (!isset($user_groups)||!is_array($user_groups))
        $user_groups = array();
      
      $key = $limit.'_'.$page;
      
      if (isset($user_groups[$key]))  {
        $groups = $user_groups[$key];
      } else {
        $g_model = AppGlobal::loadModel('GroupMembers', 'group');
        $groups = $g_model->getUserGroups($uid, $page, $limit);
        if (isset($groups)){
          $user_groups[$key] = $groups;
          $ctrl->setCache("user_groups_$uid",$user_groups,"helper_");
        }
      }
      
      return $groups;
    }
    
    public function getUserGroupCount($uid){
      $ctrl = $this->getActionController(); 
      
      $user_groups = $ctrl->getCache("user_groups_$uid","helper_");
      if (!isset($user_groups)||!is_array($user_groups))
        $user_groups = array();
      
      if (isset($user_groups["group_count"]))  {
        $count = $user_groups["group_count"];
      } else {
        $g_model = AppGlobal::loadModel('GroupMembers', 'group');
        $count = $g_model->getUserGroupCount($uid);
        if (isset($groups)){
          $user_groups["group_count"] = $count;
          $ctrl->setCache("user_groups_$uid",$user_groups,"helper_");
        }
      }
      
      return $count;
    }
    
    /**
    * check privacy by gid
    * 
    * @param mixed $gid
    * @param mixed $key
    * @param mixed $value
    */
    public function privacy($gid, $key, $value){
      $group_info = $this->getGroupInfo($gid);
      if (!isset($group_info) || !isset($group_info->$key))
        return false;
        
      return $group_info->$key==$value;
    }
    
    /**
    * check privacy by group_info
    * 
    * @param mixed $group_info
    * @param mixed $key
    * @param mixed $value
    */
    public function iprivacy($group_info,$key,$value){
      if (!isset($group_info) || !isset($group_info->$key))
        return false;
        
      return $group_info->$key==$value;      
    }
    
    public function isCanAddBookmark($gid,$uid){
      //link enable in group?
      if (!$this->privacy($gid,'link',1))
        return false;
      $member_status = $this->getMemberStatus($uid,$gid);
      if ($member_status>0)
        return true;
        
      return false;
    }
    
    public function isCanRemoveBookmark($gid,$uid){
      //link enable in group?
      if (!$this->privacy($gid,'link',1))
        return;
      $member_status = $this->getMemberStatus($uid,$gid);
      if ($member_status>1)
        return true;
      $ctrl = $this->getActionController();
      if ($ctrl->hasPerm(MOD_GROUP))
        return true;
        
      return false;
    }
}


